package top.guxx.mall.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.guxx.mall.Response;
import top.guxx.mall.constants.Constants;
import top.guxx.mall.domain.dto.CartDTO;
import top.guxx.mall.domain.dto.PaidOrderDTO;
import top.guxx.mall.domain.dto.ProductDTO;
import top.guxx.mall.domain.po.Product;
import top.guxx.mall.service.IOrderService;
import top.guxx.mall.service.IProductService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：Guxx
 * @Description：
 * @Date：2025/2/23 3:58
 * @Filename：ProductController
 */

@Slf4j
@RestController()
@CrossOrigin("*")
@RequestMapping("/api/v1/product/")
public class ProductController {
    @Resource
    private IProductService productService;

    @Resource
    private IOrderService orderService;

    @RequestMapping(value = "find_all_product", method = RequestMethod.GET)
    public Response<List<ProductDTO>> queryAllProduct(@RequestParam String userId) {
        try {
            log.info("开始获取首页商品 userId:{}", userId);
            List<ProductDTO> list = productService.queryProduct(userId);
            return Response.<List<ProductDTO>>builder()
                    .code(Constants.ResponseCode.SUCCESS.getCode())
                    .data(list)
                    .info(Constants.ResponseCode.SUCCESS.getInfo())
                    .build();
        }catch (Exception e){
            log.error("获取首页商品失败！userId:{}",userId, e);
            return Response.<List<ProductDTO>>builder()
                    .code(Constants.ResponseCode.UN_ERROR.getCode())
                    .info(Constants.ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping(value = "find_cart_info", method = RequestMethod.GET)
    public Response<List<CartDTO>> findInCartProduct(@RequestParam String userId) {
        try {
            List<CartDTO> cartDTOS = productService.queryInCart(userId);
            return Response.<List<CartDTO>>builder()
                    .code(Constants.ResponseCode.SUCCESS.getCode())
                    .data(cartDTOS)
                    .info(Constants.ResponseCode.SUCCESS.getInfo())
                    .build();
        }catch (Exception e){
            log.error("获取购物车商品失败！userId:{}",userId, e);
            return Response.<List<CartDTO>>builder()
                    .code(Constants.ResponseCode.UN_ERROR.getCode())
                    .info(Constants.ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping(value = "find_favorite_Info", method = RequestMethod.GET)
    public Response<List<CartDTO>> findFavoriteProduct(@RequestParam String userId) {
       try {
           List<CartDTO> cartDTOS = productService.queryFavorite(userId);
           return Response.<List<CartDTO>>builder()
                   .code(Constants.ResponseCode.SUCCESS.getCode())
                   .data(cartDTOS)
                   .info(Constants.ResponseCode.SUCCESS.getInfo())
                   .build();
       }catch (Exception e){
           log.error("获取收藏商品失败！userId:{}",userId, e);
           return Response.<List<CartDTO>>builder()
                   .code(Constants.ResponseCode.UN_ERROR.getCode())
                   .info(Constants.ResponseCode.UN_ERROR.getInfo())
                   .build();
       }
    }

    @RequestMapping(value = "favorite", method = RequestMethod.POST)
    public Response<String> favoriteProduct(@RequestParam String userId,
                                            @RequestParam String productId) {
        try {
            productService.add2Favorite(userId,productId);
            return Response.<String>builder()
                    .code(Constants.ResponseCode.SUCCESS.getCode())
                    .info(Constants.ResponseCode.SUCCESS.getInfo())
                    .build();
        }catch (Exception e){
            log.error("获取收藏商品失败！userId:{}",userId, e);
            return Response.<String>builder()
                    .code(Constants.ResponseCode.UN_ERROR.getCode())
                    .info(Constants.ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping(value = "in_cart", method = RequestMethod.POST)
    public Response<String> inCartProduct(@RequestParam String userId,
                                          @RequestParam String productId) {
        try {
            productService.add2Cart(userId,productId);
            return Response.<String>builder()
                    .code(Constants.ResponseCode.SUCCESS.getCode())
                    .info(Constants.ResponseCode.SUCCESS.getInfo())
                    .build();
        }catch (Exception e){
            log.error("获取购物车商品失败！userId:{}",userId, e);
            return Response.<String>builder()
                    .code(Constants.ResponseCode.UN_ERROR.getCode())
                    .info(Constants.ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping(value = "order", method = RequestMethod.GET)
    public Response<List<PaidOrderDTO>> getOrderInfo(@RequestParam String userId) {
        try {

            List<PaidOrderDTO> res = orderService.queryPaidOrder(userId);
            return Response.<List<PaidOrderDTO>>builder()
                    .code(Constants.ResponseCode.SUCCESS.getCode())
                    .info(Constants.ResponseCode.SUCCESS.getInfo())
                    .data(res)
                    .build();
        }catch (Exception e){
            log.error("获取购物车商品失败！userId:{}",userId, e);
            return Response.<List<PaidOrderDTO>>builder()
                    .code(Constants.ResponseCode.UN_ERROR.getCode())
                    .info(Constants.ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

}
